﻿using NursingFormSystem.DNNursingService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.TemperatureRecord
{
    public partial class TemperaList : Form
    {
        NursingFormSystem.WebReference2.FunctionAndPermissions WebService = new NursingFormSystem.WebReference2.FunctionAndPermissions();
        BNNursingDocsSystem bnService = new BNNursingDocsSystem();
        public TemperaList()
        {
            InitializeComponent();
            BindControl();
            BindInfo();
        }
        private void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now;
            ddlAreaName.SelectedIndex = 0;
        }

        private void BindInfo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AreaId", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("Date", typeof(string));
            dt.Columns.Add("Tempera07", typeof(string));
            dt.Columns.Add("Tempera15", typeof(string));

            DataTable patients = WebService.getDepartmentCodePatients(ddlAreaName.Text).D;
            if (patients != null)
            {
                foreach (DataRow row in patients.Rows)
                {
                    PatientInfo PatientInfo = new NursingFormSystem.PatientInfo();
                    PatientInfo.PATIENT_ID = row["PATIENT_ID"].ToString();
                    PatientInfo.VISIT_ID = Convert.ToInt32(row["VISIT_ID"]);
                    PatientInfo.PATIENT_NAME = row["PATIENT_NAME"].ToString();
                    DataTable dtSrc = bnService.GetTemperature(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, this.StatDate.DateTime.ToString("yyyy-MM-dd")).D;
                    DataRow newRow = dt.NewRow();
                    newRow["AreaId"] = ddlAreaName.Text;
                    newRow["Date"] = this.StatDate.Text;
                    newRow["PatientName"] = PatientInfo.PATIENT_NAME;
                    newRow["Tempera07"] = GetDataTableTemperaByTime(dtSrc, "7:00");
                    newRow["Tempera15"] = GetDataTableTemperaByTime(dtSrc, "15:00");
                    dt.Rows.Add(newRow);
                }
            }
            this.gridControl1.DataSource = dt;
        }

        private string GetDataTableTemperaByTime(DataTable dtSrc, string time)
        {
            string res = string.Empty;
            string whereStr = string.Format("ValueTime='{0}'", time);
            DataRow[] rows = dtSrc.Select(whereStr);
            if (rows.Length >= 1) return rows[0]["Body_Temperature"].ToString();
            return res;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            CommonPrint commp = new CommonPrint((DataTable)this.gridControl1.DataSource, "体温GRF.grf");
            commp.ShowDialog();
        }
    }
}
